﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.Mvc;

namespace Eacademy.Library.Helpers
{
    public static class StringUtilities
    {

        public static string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
        {

            string[] result = { "", "" };
            int iIndexOfBegin = strSource.IndexOf(strBegin);
            if (iIndexOfBegin != -1)
            {
                // include the Begin string if desired
                if (includeBegin)
                    iIndexOfBegin -= strBegin.Length;
                strSource = strSource.Substring(iIndexOfBegin + strBegin.Length);
                int iEnd = strSource.IndexOf(strEnd);
                if (iEnd != -1)
                {
                    // include the End string if desired
                    if (includeEnd)
                        iEnd += strEnd.Length;
                    result[0] = strSource.Substring(0, iEnd);
                    // advance beyond this segment
                    if (iEnd + strEnd.Length < strSource.Length)
                        result[1] = strSource.Substring(iEnd + strEnd.Length);
                }
            }
            else
                // stay where we are
                result[1] = strSource;
            return result;

        }

        public static string RemoveBetween(this string strSource, string strBegin, string strEnd, bool removeBegin = true, bool removeEnd = true)
        {

            while (true)
            {
                string[] result = GetStringInBetween(strBegin, strEnd, strSource, removeBegin, removeEnd);

                if (result[0] == "")
                {
                    return strSource;
                }
                else
                {
                    strSource = strSource.Replace(result[0], "");
                }
            }
            // nothing found between begin & end
            //return strSource;
        }

        public static string GenerateSlug(this string phrase, int length = 0)
        {
            string str = phrase.RemoveAccent().ToLower();

            str = Regex.Replace(str, @"[^a-z0-9\s-]", ""); // invalid chars          
            str = Regex.Replace(str, @"\s+", " ").Trim(); // convert multiple spaces into one space
            if (length > 0)
            {
                //str = str.Substring(0, str.Length <= 45 ? str.Length : 45).Trim(); // cut and trim it
                str = String.Join("-", str.SplitOnLength(80));
                return str;
            }
            str = Regex.Replace(str, @"\s", "-"); // hyphens  

            return str;
        }

        public static string RemoveAccent(this string txt)
        {
            byte[] bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt);
            return System.Text.Encoding.ASCII.GetString(bytes);
        }

        public static IEnumerable<string> SplitOnLength(this string s, int length)
        {
            var pattern = string.Format(@"^.{{0,{0}}}W", length);
            var result = Regex.Match(s, pattern).Groups[0].Value;

            if (result == string.Empty)
            {
                if (s == string.Empty) yield break;
                result = s.Substring(0, length);
            }

            yield return result;

            foreach (var subsequentResult in SplitOnLength(s.Substring(result.Length), length))
            {
                yield return subsequentResult;
            }
        }
    }
}